package com.hugh.lagouserviceuser.util;

import org.springframework.util.DigestUtils;

import java.security.MessageDigest;
import java.util.UUID;

/**
 * @Author: Hugh
 * @Date: 2020/7/19
 */
public class Md5 {

    /**
     *  加密
     * @param str 加密字符串
     * @return 加密后的内容
     */
    public static String encoderByMd5(String str){
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(str.getBytes("UTF-8"));
            StringBuilder buf = new StringBuilder("");
            int i;
            for (int offset = 0; offset < digest.length; offset++) {
                i = digest[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        }catch (Exception e){
            return UUID.randomUUID().toString();
        }
    }

    public static Boolean checkIsPasswork(String oldPassword, String newPassword){
        Boolean res = Boolean.FALSE;
        try {
            res = encoderByMd5(newPassword).equals(oldPassword);
        } catch (Exception e){

        }
        return res;
    }

    public static String getBeforeEncode(String email, String passWord) {
        return email + passWord;
    }
}
